﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加用户</title>
    <link rel="stylesheet" href="./layui/css/layui.css" media="all">
</head>

<body>
    <script src="./layui/layui.js"></script>
    <section class="layui-larry-box">
        <div class="larry-personal">
            <header class="larry-personal-tit">
                <span>添加信息</span>
            </header>
            <!-- /header -->
            <div class="larry-personal-body clearfix">
                <form class="layui-form col-lg-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-block">
                            <input type="text" name="Name" id="ID_Name" autocomplete="off" class="layui-input" placeholder="用户名">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="text" name="Password" id="ID_Password" autocomplete="off" class="layui-input" placeholder="密码">
                        </div>
                    </div>



                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="btnAddItem">添加</button>
                        <button type="reset" id="res" class="layui-btn layui-btn-primary">重置</button>
                    </div>
            </div>
            </form>
        </div>
        </div>
    </section>

    <script type="text/javascript">
        layui.use(['form', 'layer', 'laydate'], function() {
            var form = layui.form,
                $ = layui.jquery;

            //监听form表单提交事件
            form.on('submit(btnAddItem)', function(data) {
                var param = data.field; //定义临时变量获取表单提交过来的数据，非json格式

                console.log(JSON.stringify(param)); //测试是否获取到表单数据，调试模式下在页面控制台查看
                var timestamp = Date.parse(new Date());
                $.ajax({
                    type: "POST",
                    url: "/v1/users/",
                    contentType: "application/json",
                    accept: "application/json",

                    data: JSON.stringify({
                        "Name": param.Name,
                        "Password": param.Password,
                    }),

                    success: function(msg) {

                        if (msg.code == 0) {
                            layer.msg("添加成功！");
                        } else {
                            layer.msg("添加失败！" + msg.msg);
                        }

                        console.log(msg);
                    }

                });



                return false;
            }); //end form
        }); //end layui.use
    </script>

</body>

</html>